package by.jabbby.dataprovider;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import by.jabbby.Logger;

public class ChatProvider {

	private String jid;
	// private int jidId;
//	private SQLiteOpenHelper dbHelper;
	private Map<String, Long> JidIdCash;
	private Context context;

	public ChatProvider(String jid, Context context) {
		this.context = context;
		JidIdCash = new HashMap<String, Long>();
		this.jid = jid;
		/*
		 * dbHelper = new Dictionary(context); Cursor c =
		 * dbHelper.getReadableDatabase().query(Dictionary.TABLE_CONTACT, new
		 * String[] { Dictionary.T_CONT_ID }, "select from " +
		 * Dictionary.TABLE_CONTACT + " where " + Dictionary.T_CONT_JID +
		 * " = ?", new String[] { this.jid }, null, null, null); if
		 * (c.getCount() > 0) { jidId = c.getInt(0); }
		 */
	}

	public List<ChatEntry> getEntries(String to, String from) {
		
		List<ChatEntry> result = null;
		SQLiteOpenHelper dbHelper = null;
		Cursor c = null;
		try{
			 dbHelper = new Dictionary(context);
	//	String query = "select from " + Dictionary.TABLE_MESSAGE_HISTORY + " where (" + Dictionary.T_MH_FROM + " = ? AND " + Dictionary.T_MH_TO + "=? ) OR"
	//			+ "(" + Dictionary.T_MH_FROM + " = ? AND " + Dictionary.T_MH_TO + "=? )";
		String query = " (" + Dictionary.T_MH_FROM + " = ? AND " + Dictionary.T_MH_TO + "=? ) OR "
		+ "(" + Dictionary.T_MH_FROM + " = ? AND " + Dictionary.T_MH_TO + "=? )";

		
		
		String order = Dictionary.T_MH_DATE + " ASC";

		 c = dbHelper.getReadableDatabase().query(Dictionary.TABLE_MESSAGE_HISTORY, null, query, new String[] { from, to, to, from }, null, null, order);
		if (c.getCount() > 0) {
			result = new ArrayList<ChatEntry>(c.getCount());
			while (c.moveToNext()) {
				String tot = c.getString(1);
				String fromt = c.getString(2);
				Long date = c.getLong(4);
				Date tmp = new Date(date);
				String message = c.getString(3);
				ChatEntry ce = new ChatEntry(message, tmp, fromt, tot);
				result.add(ce);
			}
		} else {
			result = new ArrayList<ChatEntry>(0);
		}
		}catch(Exception e){
			Logger.e(e);
		}finally{
			try {
				c.close();
				dbHelper.close();
			} catch (Exception e2) {
				Logger.e("Error when close db connection", e2);
			}			
		}
		return result;
	}

	public void addEntry(final ChatEntry ce) {
		SQLiteOpenHelper dbHelper = null;
		try {
			dbHelper = new Dictionary(context);
			SQLiteDatabase db = dbHelper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put(Dictionary.T_MH_DATE, ce.getDate().getTime());
			values.put(Dictionary.T_MH_FROM, ce.getFrom());
			values.put(Dictionary.T_MH_MESSAGE, ce.getMessage());
			values.put(Dictionary.T_MH_TO, ce.getTo());
			db.insert(Dictionary.TABLE_MESSAGE_HISTORY, null, values);
			db.close();
		} catch (Exception e) {
			Logger.e("Error in create chat  entry", e);
		}finally{
			try{
			dbHelper.close();
			}catch (Exception e) {
				
			}
		}

			}

/*	private long getJidId(String jid) {
		Long result = JidIdCash.get(jid);
		if (result == null) {
			ContactProvider cp = new ContactProvider(context);
			ContactEntry ce = cp.getEntryOrCreate(jid);
			result = ce.getId();
		}
		return result;
	}
*/
}
